package huanghui.netstorage.util;

import huanghui.netstorage.modle.VirtualFile;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class XMLUtil 
{
	private static Document constructDocument()
	{
		Document document = DocumentHelper.createDocument();
		Element root = DocumentHelper.createElement("root");
		
		document.setRootElement(root);
		
		return document;
	}
	
/*	
	public static String constructFileInfo(VirtualFile fileInfo)
	{
		Document document = constructDocument();
		Element root = document.getRootElement();
		int state = NetStorage.ACK_FAIL;
		if (fileInfo == null)
		{
			state = NetStorage.ACK_FAIL;
		}
		else if (fileInfo.getChildFiles() == null) 
		{
			state = NetStorage.ACK_REQUESTDIR_NULL;
		}
		else
		{
			state = NetStorage.ACK_REQUESTDIR_SUCCESS;
		}
		
		Element stateElement = root.addElement("state");
		stateElement.addAttribute("value", state+"");
		
		if (state == NetStorage.ACK_REQUESTDIR_SUCCESS)
		{
			Set<VirtualFile> files = fileInfo.getChildFiles();
			Iterator<VirtualFile> iterator = files.iterator();
			while (iterator.hasNext()) 
			{
				VirtualFile dirfile = iterator.next();
				Element fileElement = root.addElement("file");
				fileElement.addAttribute("id", dirfile.getId()+"");
				fileElement.addAttribute("type", dirfile.getType()+"");
				fileElement.addAttribute("name", dirfile.getName());
				fileElement.addAttribute("size", dirfile.getSize()+"");
				fileElement.addAttribute("modifytime", System.currentTimeMillis()+"");
			}
		}
		
		//type.setText(FileTransfer.Mt_TRANSFER_File+"");
		
		//Element file = root.addElement("file");
		//constructFile(file, fileInfo);
		
		//return prettyXML(document.asXML());
		return document.asXML();
	}*/
	
	public static String constructFileInfo(List<VirtualFile> files)
	{
		Document document = constructDocument();
		Element root = document.getRootElement();
		int state = NetStorage.ACK_FAIL;
	
		if (files == null) 
		{
			state = NetStorage.ACK_REQUESTDIR_NULL;
		}
		else
		{
			state = NetStorage.ACK_REQUESTDIR_SUCCESS;
		}
		
		Element stateElement = root.addElement("state");
		stateElement.addAttribute("value", state+"");
		
		if (state == NetStorage.ACK_REQUESTDIR_SUCCESS)
		{
			
				for (VirtualFile dirfile : files) 
				{
					Element fileElement = root.addElement("file");
					fileElement.addAttribute("id", dirfile.getId()+"");
					fileElement.addAttribute("name", dirfile.getName());
					fileElement.addAttribute("type", dirfile.getType()+"");
					fileElement.addAttribute("size", dirfile.getSize()+"");
					fileElement.addAttribute("modifytime", TextUtil.formatDate(dirfile.getModifyTime()));
				}	
			
		}
		return document.asXML();
	}
	
	
	public static String constructFileInfo(VirtualFile file, int state)
	{
		Document document = constructDocument();
		Element root = document.getRootElement();
		
		Element stateElement = root.addElement("state");
		stateElement.addAttribute("value", state+"");
		if (state == NetStorage.ACK_OK && file != null) 
		{
			Element fileElement = root.addElement("file");
			fileElement.addAttribute("id", file.getId()+"");
			fileElement.addAttribute("name", file.getName());
			fileElement.addAttribute("type", file.getType()+"");	
			fileElement.addAttribute("size", file.getSize()+"");
			fileElement.addAttribute("modifytime", TextUtil.formatDate(file.getModifyTime()));
		}
		
		return document.asXML();
	}

}
